package com.aiti.lulian.utils;

import cn.hutool.core.util.IdUtil;
import com.aiti.lulian.entity.MsgInbox;
import com.aiti.lulian.server.WebSocketServer;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class NotificationUtils {

    @Autowired
    public WebSocketServer webSocketServer;

    @Async
    public void saveAndSendMessage(String fromUserId,String sendUserId, String content) {
        MsgInbox msgInbox = new MsgInbox() ;
        msgInbox.setReceiverId(sendUserId);
        msgInbox.setStatus(0);
        msgInbox.setSenderId(fromUserId);
        msgInbox.setBaseId(IdUtil.simpleUUID());
        msgInbox.setContent(content);
        try{
            webSocketServer.saveAndSendMessage(JSONObject.toJSONString(msgInbox));
        }catch (Exception e){
            log.error("发送消息失败",e.getMessage());
        }

    }
}
